<?php

namespace App\Services\impl;

use App\Services\spider\IUrParser;
use Illuminate\Support\Str;

class QgyydqUrlParser extends IUrParser
{

    public function isNeedUrl(?string $url): bool
    {
        return $url && Str::startsWith($url, "https://shuju.qgyyzs.net");
    }

    public function filterATagHref(string $href, ?string $oriUrl): ?string
    {
        $resultUrl = null;
        if (Str::startsWith($href, "https://shuju.qgyyzs.net")) {
            $resultUrl = $href;
        } elseif (Str::startsWith($href, "/")) {
            $resultUrl = "https://shuju.qgyyzs.net" . $href;
        } elseif ($oriUrl && Str::startsWith($href, "?") && Str::contains($href, "key=")) {
            $resultUrl = Str::substr($oriUrl, 0, strpos($oriUrl, "?")) . $href;
        }
        return $resultUrl;

    }
}